package com.joker.order.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.alipay.api.response.AlipayTradePagePayResponse;
import com.joker.cart.remote.IPmsCartRemoteService;
import com.joker.common.core.constant.Constants;
import com.joker.common.core.domain.Result;
import com.joker.order.config.PayConfig;
import com.joker.order.mapper.PayWaterMapper;
import com.joker.order.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lemon
 * @date 2024/12/30 21:04
 **/
@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    private AlipayConfig alipayConfig;
    @Autowired
    private PayConfig payConfig;

    @Autowired
    private IPmsCartRemoteService remoteService;
    @Autowired
    private PayWaterMapper payWaterMapper;

    @Override
    public String generatePay(String totalPrice, Long[] skuIds) throws AlipayApiException {

        Result result = remoteService.removePayed(skuIds);
        if (result.getCode()== Constants.ERROR) {
            System.out.println(result.getMsg());
        }

        // 1. 根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置商户订单号
        model.setOutTradeNo(RandomUtil.randomNumbers(5));
        // 设置订单总金额
        model.setTotalAmount(totalPrice);
        // 设置订单标题
        model.setSubject("cart_pay");
        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        // 设置PC扫码支付的方式
        model.setQrPayMode("1");
        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(200L);

        // 设置订单包含的商品列表信息
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.add(Calendar.MINUTE,30);
        Date time = instance.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置订单绝对超时时间
        model.setTimeExpire(simpleDateFormat.format(time));

        request.setBizModel(model);
        // 设置支付成功后的返回地址
        request.setReturnUrl(payConfig.getReturnUrl());
        request.setNotifyUrl(payConfig.getNotifyUrl());
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return pageRedirectionData;
    }



}
